package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.service.ISignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 不出bug- 么么哒
 * @DESCRIPTION
 * @create 2018/11/15
 */
@RestController
@RequestMapping("/signs")
public class SignController {
    @Autowired
    private ISignService signService;

    /**
     * 抽奖随机概率
     *
     * @return
     */
    @GetMapping("/randomnums")
    public Object getRandomnums() {
        double p1 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        double p2 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        double p3 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        double p4 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        double p5 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        double p6 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        double p7 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        double p8 = new BigDecimal (1 - p1 - p2 - p3 - p4 - p5 - p6 - p7).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        while (p8 <= 0) {
            p1 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
            p2 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
            p3 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
            p4 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
            p5 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
            p6 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
            p7 = new BigDecimal (Math.random ()).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
            p8 = new BigDecimal (1 - p1 - p2 - p3 - p4 - p5 - p6 - p7).setScale (2, BigDecimal.ROUND_HALF_UP).doubleValue ();
        }
        List<Double> ps = new ArrayList<> ();
        ps.add (p1);
        ps.add (p2);
        ps.add (p3);
        ps.add (p4);
        ps.add (p5);
        ps.add (p6);
        ps.add (p7);
        ps.add (p8);
        return ps;
    }

    /**
     * 查询今日是否签到
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public Object getSign(@PathVariable Long userId) {
        return signService.getTodaySignByUserId (userId);
    }

    /**
     * 查询所有签到信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId}/results")
    public Object getSigns(@PathVariable Long userId) {
        return signService.getSignsByUserId (userId);
    }

    @PostMapping
    public Object saveOrUpdate(Sign sign) {
        signService.saveSignInfo (sign);
        return null;
    }
}
